package daoImpl;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import pojo.Alarm;
import pojo.Car;
import pojo.Distance;
import pojo.User;
import dao.UserDao;

public class UserDaoImpl extends HibernateDaoSupport implements UserDao{

	@Override
	public User login(User user) {
		
		/*DetachedCriteria dc = DetachedCriteria.forClass(User.class);
		if(user!=null){
			if(user.getUsername()!=null && user.getUsername().length()>0){
				dc.add(Restrictions.eq("username", user.getUsername()));
			}
			if(user.getPwd()!=null && user.getPwd().length()>0){		
				dc.add(Restrictions.eq("pwd", user.getPwd()));
			}
		}		
		List list = this.getHibernateTemplate().findByCriteria(dc);
		if(list!=null && list.size()>0){
			return true;
		}else{
			return false;
		}*/
		String username = user.getUsername();
		String pwd = user.getPwd();
		if(username==null || "".equals(username)){
			return null;
		}
		if(pwd==null || "".equals(pwd)){
			return null;
		}
		List list = this.getHibernateTemplate().find("from User where username='"+username+"'");
		if(list.size()==0){
			return null;
		}else{
			return (User) list.get(0);
		}
		
	}


	@Override
	public void save(User user) {
		this.getHibernateTemplate().save(user);
	}

	@Override
	public Car queryByUsername(String username) {
		if(username ==null || "".equals(username.trim())){
			 return null;
		}
		List<Car> list = this.getHibernateTemplate().find("from Appuser where username=?",username);
		if(list.size()==0){
			return null;
		}else{
			return list.get(0);
		}
		
	}

	@Override
	public Car bind(Car car) {
	    this.getHibernateTemplate().save(car);
	    return car;
	
	}

	//根据车的编号查询，如果已经存在就返回
	@Override
	public Car query(String username) {
		if(username ==null || "".equals(username.trim())){
			 return null;
		}
		List<Car> list = this.getHibernateTemplate().find("from Car where username=?",username);
		if(list.size()==0){
			return null;
		}else{
			return list.get(0);
		}
		
	}


	/* 
	 * @see dao.UserDao#delete(java.lang.String)
	 */
	@Override
	public void dele(String username) {
		// TODO Auto-generated method stub
		if(!"".equals(username.trim()) && username!=null){
			Car car = this.getHibernateTemplate().load(Car.class, username);
			this.getHibernateTemplate().delete(car);
		}
	}


	/* 查询绑定历史记录
	 * @see dao.UserDao#queryList(java.lang.String)
	 */
	@Override
	public List<User> queryList(String binder) {
		if(binder==null || "".equals(binder.trim())){//监护人为空
		return null;
		}
		List<User> list = this.getHibernateTemplate().find("from Watchuser where binder='"+binder+"'");
		if(list.size()==0){
			return null;
		}else{
			return list;
		}
		
	}


	/* (non-Javadoc)
	 * @see dao.UserDao#queryCarList(java.lang.String)
	 */
	@Override
	public List<Car> queryCarList(String binder) {
		//String hql="select m.username from (select c.username from Car c inner join User u on c.binder=u.username where binder='"+binder+"') m";
		if(binder==null || "".equals(binder.trim())){//监护人为空
			return null;
			}

		List<Car> list = this.getHibernateTemplate().find("from Car where binder='"+binder+"'");
		if(list.size()==0){
			return null;
		}else{
			return list;

		}
	}


	/* 
	 * @see dao.UserDao#update(pojo.Car)
	 */
	@Override
	public Car update(Car car) {
		String username = car.getUsername();
		String type = car.getType();
		String color = car.getColor();
		String description = car.getDescription();
		String remark = car.getRemark();
		String hql="update Car set";
		if(type==null || "".equals(type.trim())){
			return null;
		}else{
			hql +=" type='"+type+"'";
		}
		if(color==null || "".equals(color.trim())){
			return null;
		}else{
			hql +=",color='"+color+"'";
		}
		if(description!=null || !"".equals(description.trim())){
			hql +=",description='"+description+"'";
		}
		if(remark!=null || !"".equals(remark.trim())){
			hql +=",remark='"+remark+"'";
		}
		if(username==null || "".equals(username.trim())){
			return null;
		}else{
			hql +="WHERE username='"+username+"' ";
		}
		Session session = this.getHibernateTemplate().getSessionFactory().openSession();
		Query query = session.createQuery(hql);
		int i = query.executeUpdate();
		if(i<=0){
			return null;
		}else{
			return car;
		}

	}



	/* 
	 * @see dao.UserDao#queryAllAlarm(pojo.Alarm)
	 */
	@Override
	public List<Alarm> queryAllAlarm(String username) {
		
		String hql="from Alarm where 1=1";
	
		if(username==null || "".equals(username.trim())){
			return null;
		}else{
		 hql +=" and where username = '"+username+"'";

		}
		return  this.getHibernateTemplate().find(hql);
	}

	/* 
	 * @see dao.UserDao#queryDistance(pojo.Distance)
	 */
	@Override
	public List<Distance> queryDistance(String entity_name,int year,int mon) {

		String hql="from Distance where 1=1";
		if(entity_name==null || "".equals(entity_name.trim())){
			return null;
		}else{
			hql +=" and entity_name='"+entity_name+"'";
		}
		if(year <=0){
			return null;
		}else{
			hql +="and year='"+year+"'";
		}
		if(mon <1){
			return null;
		}else{
			hql +="and mon='"+mon+"'";
		}
	return  this.getHibernateTemplate().find(hql);
	
	}


	/* 
	 * @see dao.UserDao#getAlarmList(java.lang.String, java.lang.String)
	 */
	@Override
	public List getAlarmList(String page, String rows) throws Exception {
        
      //当为缺省值的时候进行赋值  
      int currentpage = Integer.parseInt((page == null || page == "0") ? "1": page);//第几页  
      int pagesize = Integer.parseInt((rows == null || rows == "0") ? "10": rows);//每页多少行  
        
      List list = this.getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery("from Alarm")  
                     .setFirstResult((currentpage - 1) * pagesize).setMaxResults(pagesize).list();  

      return list;  
	}


	/* 
	 * @see dao.UserDao#getAlarmTotal()
	 */
	@Override
	public int getAlarmTotal() throws Exception {
		return this.getHibernateTemplate().getSessionFactory().getCurrentSession().find("from Alarm").size();
	}



}
